<template xmlns:v-clipboard="">
  <div id="dialogQrcode">
    <el-dialog
      class="about-set"
      title="预览"
      :visible.sync="dialogData.isShow"
      width="400px"
      :before-close="labelHandleClose"
    >
      <div class="con">
        <h1>手机预览</h1>
        <p class="pre__text">（请打开手机微信“扫一扫”，扫码后可预览）</p>
        <!-- <qrcode-vue :value="inputValue" size="130" level="H"></qrcode-vue> -->
        <img :src="qrCode" alt="" />
      </div>
      <div class="pre__copy-link">
        复制链接：
        <el-input :value="inputValue"></el-input>
        <el-button
          size="mini"
          v-copy="inputValue"
          @click="vCopyText(inputValue)"
          >复制</el-button
        >
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" @click="cancelLabel">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import QRCode from 'qrcode'
// import qrcodeVue from 'qrcode.vue' //二维码
import { copy } from 'v-copy'
export default {
  name: 'dialogQrcode',
  msg: '阅读-移动端预览',
  props: ['dialogData'],
  components: {
    // qrcodeVue
  },
  directives: {
    copy
  },
  data() {
    return {
      inputValue: '',
      qrCode: ''
    }
  },
  mounted() {
    this.inputValue = this.dialogData.linkUrl
    this.getQRcode(this.inputValue).then((img) => {
      this.qrCode = img
    })
  },
  methods: {
    async getQRcode(url) {
      const cofing = {
        errorCorrectionLevel: 'L',
        type: 'image/jpeg',
        quality: 1,
        margin: 1,
        color: {
          dark: '#000',
          light: '#fff'
        }
      }
      try {
        const qrcode = await QRCode.toDataURL(url, cofing)
        return qrcode
      } catch (err) {
        console.error(err)
      }
    },
    //复制提示
    vCopyText(value) {
      if (value) {
        this.$message.success('已复制！')
      } else {
        this.$message.warning('无数据！')
      }
    },
    labelHandleClose(done) {
      this.cancelLabel()
      done()
    },
    cancelLabel() {
      // 关闭前清空
      this.dialogData.isShow = false
      this.dialogData.linkUrl = ''
    }
  }
}
</script>

<style lang="less">
#dialogQrcode {
  .con {
    color: #212529;
    text-align: center;
    h1 {
      font-size: 24px;
      padding-top: 10px;
      margin-bottom: 24px;
    }
    .pre__text {
      font-size: 16px;
      margin-bottom: 24px;
    }
  }
  .pre__copy-link {
    margin: 20px 0 30px;
    .el-input {
      display: inline-block;
      width: 219px;
    }
    .el-input__inner {
      border: none;
      border-bottom: 1px solid #dcdfe6;
      border-radius: 0;
      height: 30px;
      line-height: 30px;
      padding: 0 5px;
    }
    .el-button {
      margin-left: 10px;
    }
  }
}
</style>
